@file:Suppress("DEPRECATION")

package com.loan.market.util

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding

fun Activity.statusBarFont(isLight: Boolean) {
    if (!isLight) {
        window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    } else {
        window.decorView.systemUiVisibility = window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
    }
}

fun Activity.navigationBarFont(isLight: Boolean) {
    if (!isLight) {
        window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
    } else {
        window.decorView.systemUiVisibility = window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
    }
}

fun Activity.transparentStatusBar() {
    window.statusBarColor = Color.TRANSPARENT
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}

fun Activity.transparentNavigationBar() {
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
}

fun Context.getStatusBarHeight(def: Int = 30f.dp2px()): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (resourceId > 0) {
        runCatching { resources.getDimensionPixelSize(resourceId) }.getOrDefault(def)
    } else {
        def
    }
}

fun ViewGroup.setStatusBarPadding() {
    updatePadding(top = context.getStatusBarHeight())
}

fun View.setStatusBarHeight() {
    updateLayoutParams<MarginLayoutParams> { height = context.getStatusBarHeight() }
}